package controllers.listeners.semrep;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import models.GenerateTranslationModel;
import models.LexiconModel;
import objects.Constituent;
import views.MainPanel;

/* Translates the concepts into its lexicon counterparts */
/* actionPerformed when you click the "Translate" button */
public class TranslateListener implements ActionListener {
	
	private MainPanel view;
	
	public TranslateListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Constituent constituent = view.getConstituent();
		
		/* Exit if there is no constituent */
		if (constituent == null) {
			JOptionPane.showMessageDialog(null, "Please enter a semantic representation to be generated!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		/* Exit if lexicon is empty */
		if (LexiconModel.getLexicon() == null || LexiconModel.getLexicon().getLexiconList() == null || LexiconModel.getLexicon().getLexiconList().size() <= 0) {
			JOptionPane.showMessageDialog(null, "Please select a language/lexicon!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		/* Clear fields */
		view.clearGenerated();
		
		/* Get generated string */
		String translation = GenerateTranslationModel.generateTranslation(constituent);
		
		/* Display generated string */
		view.setTextSentenceValue(translation);
		
		view.revalidate();
	}
	
	public void setView(MainPanel view){
		this.view = view;
	}
	
}
